# 项目:
# 文件: demo5
# 作者: Yang
# 日期: 2025/5/23 14:25
# coding:utf-8
from selenium.webdriver.support.expected_conditions import element_to_be_selected

from pyobj.tools import Util


class KeyWord:

    driver = None

    def open_browser(self,browser):         #打开浏览器
        from selenium import webdriver
        if hasattr(webdriver,browser):
            self.driver = getattr(webdriver,browser)()
            self.driver.implicitly_wait(5)  # 隐式等待
            self.driver.maximize_window()  # 最大化窗口

    def open_page(self,url):        # 打开页面
        self.driver.get(url)

    def input(self,what,value):     #输入信息
        element = self.driver.find_element('css selector',what)
        element.click()
        element.clear()
        element.send_keys(value)

    def click(self,what):           #点击
        self.driver.find_element('css selector',what).click()

    def sleep(self,second):     #休眠方法
        import time
        time.sleep(int(second))

    def refresh(self):
        self.driver.refresh()

    def find_ele(self,what):
        try:
            ele = self.driver.find_element('css selector',what)
            actual = ele.text
        except Exception as e:
            actual = 'not found'
        return actual

    def assert_equal(self,what,expect):
        actual = self.find_ele(what)
        assert actual == expect

    def close(self):            #关闭
        self.driver.close()

def test_run():
    scripts = Util.read_csv('smscript.csv')  # 获取脚本
    keymap = Util.read_json('key.json')  # 获取json文件
    kw = KeyWord()
    for step in scripts:
        key = step.split(', ')[0]
        ekey = keymap[key]  # 获取英文的键
        params = step.split(', ')[1:]
        if hasattr(kw, ekey):
            getattr(kw, ekey)(*tuple(params))


if __name__ == '__main__':
    test_run()